package net.lqy.aipan.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @title: TimeCostFilter
 * @Author lqy
 * @Date: 2025-06-07 14:56
 * @Version 1.0
 */
@Slf4j
@WebFilter(urlPatterns = "/*") // 拦截所有请求
public class TimeCostFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        try {
            chain.doFilter(request, response); // 放行请求
        } finally {
            long cost = System.currentTimeMillis() - startTime;
            String uri = ((HttpServletRequest) request).getRequestURI();
            log.info("接口耗时监控 | URI: {} | 耗时: {}ms", uri, cost);
        }
    }
}
